package org.bluedream.core.module.service.emp;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import org.apache.commons.collections.map.HashedMap;
import org.bluedream.comm.base.BaseEntity;
import org.bluedream.comm.utils.EmptyUtil;
import org.bluedream.comm.utils.RestResult;
import org.bluedream.comm.utils.StringUtils;
import org.bluedream.core.module.base.OrgBaseServiceImpl;
import org.bluedream.core.module.dao.emp.EmpDao;
import org.bluedream.core.module.entity.emp.Emp;
import org.bluedream.core.utils.UserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName EmpService
 * @Description TODO
 * @Author Administrator
 * @Data 2022/7/20 10:16
 * @Version
 */
@Service
@DS("master")
@Transactional(
        readOnly = true
)
public class EmpService extends OrgBaseServiceImpl<EmpDao, Emp> implements IService<Emp> {
    private static final Logger logger = LoggerFactory.getLogger(EmpService.class);

    @Override
    public Emp getById(Serializable id) {
        if (EmptyUtil.isEmpty(id)){
            return new Emp();
        }
        return super.getById(id);
    }

    public IPage<Emp> findPage(Emp entity , String... args){
        Map<String , Object> search = new HashedMap();
        return super.findPage(entity , true , search , args);
    }

    public List<Map<String , Object>> treeData(){
        try {
            List<Map<String , Object>> mapList = new ArrayList<>();
            QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("org_code" , UserUtil.getLoginOrgCode());
            List<Emp> empList = super.list(queryWrapper);
            for (Emp v0:empList
            ) {
                if (!BaseEntity.STATUS_NORMAL.equals(v0.getStatus())){
                    continue;
                }
                Map<String , Object> map = new HashMap<>();
                String id = v0.getId();

                map.put("id" , id);
                map.put("pId" , "0");
                map.put("code" , v0.getCode());
                map.put("name" , StringUtils.getTreeNodeName("false" , v0.getCode() , v0.getName()));
                map.put("isTreeLeaf" , true);
                map.put("isParent" , false);
                mapList.add(map);
            }
            return mapList;
        }catch (Exception e1){
            e1.printStackTrace();
        }
        return null;
    }

    @Transactional(
            readOnly = false,
            rollbackFor = Exception.class
    )
    public RestResult saveEntity(Emp entity){
        try{
            boolean flag = super.save(entity);
            return RestResult.ok();
        }catch (Exception e1){
            return RestResult.error(e1.getMessage());
        }
    }
}